﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSound = Microsoft.DirectX.DirectSound;
using D3D = Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Drawing;

namespace EdytorWF.Objects
{
    public class SoundObject : ModelObject
    {
        private static int counter = 0;

        DSound.Device MySoundDevice;
        private DSound.SecondaryBuffer MYSecondBuffer { get; set; }
        private DSound.Buffer3D MyBuffer3D { get; set; }
        private DSound.Buffer MyBuffer { get; set; }
        private DSound.Listener3D MyListener3D;
        
        private String _SoundFile;
        public String SoundFile
        {
            get
            {
                return _SoundFile;
            }
            set
            {
                _SoundFile = value;
                ReloadSound();
            }
        }

        public float DistanceFactor { get; set; }
        public float MinDistance { get; set; }
        public float MaxDistance { get; set; }
        public int SoundVolumne { get; set; }

        public DSound.Listener3D GetListener() { return MyListener3D; }

        public SoundObject(D3D.Device device,  DSound.Device s_device)
        {
            Device = device;
            MySoundDevice = s_device;

            TypeName = "_SOUND_OBJECT";
            ObjectName = String.Format("SoundObject{0}", counter);
            ++counter;

            MeshFileName = IEngine.EngineInstance.DefaultSoundMeshName;
            TextureFileName = IEngine.EngineInstance.DefaultSoundTextureName;

            LoadMesh(MeshFileName);
            LoadTexture(TextureFileName);

            DistanceFactor = 200;
            MinDistance = 1;
            MaxDistance = 3000;
            SoundVolumne = 0;

            SoundFile = IEngine.EngineInstance.DefaultSound;
            BufferDescription BD = new BufferDescription();

            BD.ControlEffects = false;
            BD.Control3D = true;
            BD.ControlVolume = true;

            MYSecondBuffer = new SecondaryBuffer(SoundFile, BD, MySoundDevice);
            MYSecondBuffer.Volume = -1;

            MyBuffer3D = new Buffer3D(MYSecondBuffer);
            MyBuffer3D.MinDistance = MinDistance;
            MyBuffer3D.MaxDistance = MaxDistance;

            BufferDescription BDL = new BufferDescription();
            BDL.ControlEffects = false;
            BDL.Control3D = true;
            BDL.PrimaryBuffer = true;
            MyBuffer = new DSound.Buffer(BDL, MySoundDevice);

            MyListener3D = new Listener3D(MyBuffer);
            MyListener3D.DistanceFactor = DistanceFactor;

            MyBuffer3D.Position = Position;

            MYSecondBuffer.Play(0, BufferPlayFlags.Looping);
        }

        public override void Move(Vector3 new_position)
        {
            base.Move(new_position);

            Position = new_position;
            MyBuffer3D.Position = new_position;
        }

        public void ReloadSound()
        {
            if (MYSecondBuffer == null)
                return;

            BufferDescription BD = new BufferDescription();

            BD.ControlEffects = false;
            BD.Control3D = true;
            BD.ControlVolume = true;

            MYSecondBuffer.Stop();
            MYSecondBuffer.Dispose();
            MYSecondBuffer = new SecondaryBuffer(SoundFile, BD, MySoundDevice);
            MYSecondBuffer.Volume = -1;

            MyBuffer3D = new Buffer3D(MYSecondBuffer);
            MyBuffer3D.MinDistance = MinDistance;
            MyBuffer3D.MaxDistance = MaxDistance;

            MyBuffer3D.Position = Position;  
            MYSecondBuffer.Play(0, BufferPlayFlags.Looping);
        }
    }   
}
